package com.jack.jetpackapp.activity

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.graphics.Color
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateInterpolator
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.library.GlobalViewModel
import com.jack.jetpackapp.R
import com.jack.jetpackapp.viewmodel.UserModel
import com.jack.uikit.adapter.BaseAdapter
import com.jack.uikit.expand.toast
import kotlinx.android.synthetic.main.activity_animator.*

/**
 * @author Jack 2021-05-23 16:50
 */
class AnimatorActivity : AppCompatActivity() {
    private lateinit var mAdapter: BaseAdapter
    private lateinit var mUserModel: UserModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_animator)
        mUserModel = GlobalViewModel.get().getViewModel(UserModel::class.java)
        mUserModel.userInfo.observe(this) {
            toast("name=${it.name};age=${it.age}")
        }
        btnAnim.setOnClickListener {
            startAnimator(image) {

            }
        }
        initAdapter()
    }

    private fun initAdapter() {
        val data = mutableListOf<Int>()
        for (i in 0 .. 30) {
            data.add(i)
        }
        mAdapter = object: BaseAdapter() {

            override fun layout(): Int = 0

            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
                val textView = TextView(parent.context)
                val layoutParams = ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.MATCH_PARENT, 300)
                layoutParams.bottomMargin = 20
                textView.layoutParams = layoutParams
                textView.gravity = Gravity.CENTER
                textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15f)
                textView.setBackgroundColor(Color.BLUE)
                return Holder(textView)
            }

            override fun onBindViewHolder(holder: Holder, position: Int) {
                (holder.itemView as TextView).text = "item${data[position]}"
                holder.itemView.setOnClickListener {
                    startAnimator(it) {
                        data.removeAt(position)
                        notifyDataSetChanged()
                    }
                }
            }

            override fun getItemCount(): Int = data.size

        }
        recyclerview.adapter = mAdapter
        recyclerview.layoutManager = LinearLayoutManager(this)
    }

    private fun startAnimator(view: View, onComplete: () -> Unit) {
        val animX = ObjectAnimator.ofFloat(view, "translationX", -resources.displayMetrics.widthPixels.toFloat())
        val animY = ObjectAnimator.ofFloat(view, "translationY", resources.displayMetrics.heightPixels.toFloat())
        val animSet = AnimatorSet()
        animSet.duration = 300
        animSet.play(animX).with(animY)
        animSet.interpolator = AccelerateInterpolator()
        animSet.addListener(object: Animator.AnimatorListener {
            override fun onAnimationStart(p0: Animator?) {

            }

            override fun onAnimationEnd(p0: Animator?) {
                toast("动画执行结束")
                onComplete.invoke()
            }

            override fun onAnimationCancel(p0: Animator?) {

            }

            override fun onAnimationRepeat(p0: Animator?) {

            }

        })
        animSet.start()
    }
}